/**
 * 图片业务接口
 */

import request from '../lib/js/request'
import { BASE_URL } from './config'
import { error } from '../lib/js/utils'

const app = getApp()

/**
 * 上传约拍图片接口
 * promise 风格
 *
 * @param {微信临时文件路径} tempFilePath
 */
export function uploadImage(tempFilePath) {
  return new Promise(function(resolve, reject) {
    wx.uploadFile({
      url: `${BASE_URL}/image/uploadImage`,
      filePath: tempFilePath,
      name: 'files',
      header: {
        Authorization: app.globalData.token
      },
      success: function(res) {
        if (res.statusCode == 200) {
          var r = JSON.parse(res.data)
          if (r.code == 0) {
            error(r.message)
          } else {
            resolve(r)
          }
        } else {
          error(res.data)
        }
      },
      fail: function() {
        error(res.data)
      }
    })
  })
}
